﻿using UnityEngine;
using System.Collections;

public class ArrowBehaviour : MonoBehaviour {

    public Vector3 destinity;
    private Vector3 speed;
    public float arrowSpeed;
    private bool ready;
	public bool canGet = false;

    public void InsertAngle()
    {
		speed.x = 1;
		speed.y = 1;
        speed.z = 0;
	
		transform.LookAt(new Vector3(destinity.x,destinity.y,transform.position.z));
		transform.Rotate(0,90,90);

        ready = true;
    }

	void Update () 
    {
        if (ready)
        {
			transform.Translate(new Vector3(0,arrowSpeed*Time.deltaTime,0),Space.Self);

            if (Mathf.Abs(destinity.x - transform.position.x) < .2)
            {
                speed.x = 0;
            }
            if (Mathf.Abs(destinity.y - transform.position.y) < .2)
            {
                speed.y = 0;
            }

            if (speed.x == 0 && speed.y == 0)
            {
				ready = false;
				canGet = true;

            }
        }
	}
	void OnTriggerEnter2D(Collider2D collider)
	{
		if(collider.name == "EnemyTrigger" && ready)
		{
			collider.gameObject.transform.parent.gameObject.SendMessage("Die",SendMessageOptions.RequireReceiver);
			canGet = true;
			ready = false;
		}
		if(collider.name == "King")
		{
			collider.gameObject.SendMessage("Die",SendMessageOptions.RequireReceiver);
			canGet = true;
			ready = false;
		}
	}

}
